﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace calculator
{
    public partial class Form1 : Form
    {
        string input="";
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            input = input + "1";
            label1.Text = input;
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void flowLayoutPanel5_Paint(object sender, PaintEventArgs e)
        {

        }

        private void flowLayoutPanel11_Paint(object sender, PaintEventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            input = input + "2";
            label1.Text = input;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            input = input + "3";
            label1.Text = input;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            input = input + "4";
            label1.Text = input;
        }

        private void button5_Click(object sender, EventArgs e)
        {
            input = input + "5";
            label1.Text = input;
        }

        private void button6_Click(object sender, EventArgs e)
        {
            input = input + "6";
            label1.Text = input;
        }

        private void button7_Click(object sender, EventArgs e)
        {
            input = input + "7";
            label1.Text = input;
        }

        private void button8_Click(object sender, EventArgs e)
        {
            input = input + "8";
            label1.Text = input;
        }

        private void button9_Click(object sender, EventArgs e)
        {
            input = input + "9";
            label1.Text = input;
        }

        private void button10_Click(object sender, EventArgs e)
        {
            input = input + "0";
            label1.Text = input;
        }

        private void button11_Click(object sender, EventArgs e)
        {
            input = input + "+";
            label1.Text = input;
        }

        private void button12_Click(object sender, EventArgs e)
        {
            input = input + "-";
            label1.Text = input;
        }

        private void button13_Click(object sender, EventArgs e)
        {
            input = input + "×";
            label1.Text = input;
        }

        private void button14_Click(object sender, EventArgs e)
        {
            input = input + "÷";
            label1.Text = input;
        }

        private void button16_Click(object sender, EventArgs e)
        {
            input = "";
            label1.Text = input;
        }

        private void button15_Click(object sender, EventArgs e)
        {
            label1.Text = input + "=" + calculating(input);
            //label1.Text = calculating(input);
            
        }
        public static string calculating(string input)
        {
            string result;
            string b;
            List<string> a = new List<string>();
            for(int i=0,j=0;i<input.Length;i++)
            {
                if(stringJudge(input[i].ToString()))
                {
                    b = input.Substring(j, i - j);
                    a.Add(b);
                    b = input[i].ToString();
                    a.Add(b);
                    j = i + 1;
                }
                if(i==input.Length-1)
                {
                    b = input.Substring(j, i - j+1);
                    a.Add(b);
                }
            }
            /*for(int i=0;i<a.Count;i++)
            {
                result = result + a[i] + " ";
            }
            return result;*/
            for(int k=0;k<a.Count;k++)
            {
                if(a[k]== "×")
                {
                    double a1 = double.Parse(a[k - 1]);
                    double a2 = double.Parse(a[k + 1]);
                    double a3 = a1 * a2;
                    a[k - 1] = a3.ToString();
                    a.RemoveAt(k+1);
                    a.RemoveAt(k);
                    k = 0;
                }
                if (a[k] == "÷")
                {
                    double a1 = double.Parse(a[k - 1]);
                    double a2 = double.Parse(a[k + 1]);
                    double a3 = a1 / a2;
                    a[k - 1] = a3.ToString();
                    a.RemoveAt(k + 1);
                    a.RemoveAt(k);
                    k = 0;
                }
            }
            for (int k = 0; k < a.Count; k++)
            {
                if (a[k] == "+")
                {
                    double a1 = double.Parse(a[k - 1]);
                    double a2 = double.Parse(a[k + 1]);
                    double a3 = a1 + a2;
                    a[k - 1] = a3.ToString();
                    a.RemoveAt(k + 1);
                    a.RemoveAt(k);
                    k = 0;
                }
                if (a[k] == "-")
                {
                    double a1 = double.Parse(a[k - 1]);
                    double a2 = double.Parse(a[k + 1]);
                    double a3 = a1 - a2;
                    a[k - 1] = a3.ToString();
                    a.RemoveAt(k + 1);
                    a.RemoveAt(k);
                    k = 0;
                }
            }
            result = a[0];
            return result;
        }
        public static bool stringJudge(string i)
        {
            if(i=="+"||i=="-"||i== "×"||i== "÷")
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}
